<template>
	<div class="view">
		<!-- <template>
			<Top :title="'留言列表'"></Top>
		</template> -->
		<div class="header">
			<div class="h_exit" @click="toIndex">
				<i class="i-exit i-20 i-mr-10"></i>
				<div class="h_str">返回上一页</div>
			</div>
			<div class="h_con">
				<div class="h_li">场地预约</div>
			</div>
		</div>

		<div class="main">
			<div class="main_con">
				<div class="m_title">场地预约</div>
				<div class="line">
					<div class="search">
						<el-input placeholder="请输入关键词" suffix-icon="el-icon-search" v-model="keywords"
							@change="search" size="medium" style="width: 200px;">
						</el-input>
						<el-button type="primary" size="medium" @click="search">搜索</el-button>
						<el-button icon="el-icon-setting" type="primary" size="medium" @click="add()">场地设置</el-button>
					</div>
				</div>

				<div class="table">
					<el-table style="width: 100%;" :data="dataList" :height="table_height">
						<el-table-column type="selection" min-width="5%" align="center">
						</el-table-column>
						<el-table-column type="index" :index="1" label="行号" min-width="10%" align="center">
						</el-table-column>
                        <el-table-column prop="type" label="角色" min-width="15%" align="center">
							<template slot-scope="scope">
								<span>{{ scope.row.type==1?"企业":"求职者" }}</span>
							</template>
						</el-table-column>
                        <el-table-column prop="name" label="姓名" min-width="20%" align="center">
							<template slot-scope="scope">
								<span>{{ scope.row.isNiming==1?"匿名":scope.row.name }}</span>
							</template>
						</el-table-column>
                        <el-table-column prop="tel" label="联系方式" min-width="20%" align="center">
						</el-table-column>
						<el-table-column prop="yongtu" label="用途" min-width="20%" align="center">
						</el-table-column>
						<el-table-column prop="timeRiqi" label="租用日期" min-width="20%" align="center">
						</el-table-column>
						<el-table-column prop="timeStart" label="开始时间" min-width="20%" align="center">
						</el-table-column>
						<el-table-column prop="timeEnd" label="结束时间" min-width="20%" align="center">
						</el-table-column>
						<el-table-column prop="isContact" label="是否联系" min-width="15%" align="center">
							<template slot-scope="scope">
								<el-switch v-model="scope.row.isContact" active-color="#13ce66" inactive-color="#A5ADBA"
									:active-value="1" :inactive-value="0" @change="isOkHandler(scope.row)">
								</el-switch>
							</template>
						</el-table-column>
						<el-table-column prop="teacher" label="操作" min-width="30%" align="center">
							<template slot-scope="scope">
								<!-- <el-button size="mini" plain @click="edit(scope.row)">编辑</el-button> -->
								<el-button size="mini" plain @click="remove(scope.row)">移除</el-button>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<div class="item_bottom">
					<div class="item_num">共{{ total }}条信息</div>
					<div class="item_page">
						<el-pagination background layout="prev, pager, next" :total="total"
							@current-change="handleCurrentChange">
						</el-pagination>
					</div>
				</div>
			</div>
		</div>
		<el-drawer :visible.sync="drawer" :direction="direction" size="50%" :wrapperClosable="false">
			<template slot="title">
				<div class="h_exit" @click="drawer = false">
					<i class="i-16 i-exit i-mr-6"></i>返回
				</div>
			</template>
			<div class="d_container">
				<el-form label-position="top" ref="form" :model="form" :rules="rules">
					<div class="main_view">
						<!-- <div class="v_title">课程信息</div> -->
						<div class="v_box">
							<el-form-item label="场地名称" prop="title">
								<!-- <el-input maxlength="25" v-model="form.title" placeholder="请输入标题" show-word-limit style="width: 400px;"></el-input> -->
								<el-input v-model="form.title" placeholder="请输入场地名称" style="width: 400px;"></el-input>
							</el-form-item>
							<el-form-item label="地址" prop="addr">
								<el-input placeholder="请输入地址" v-model="form.addr" style="width: 400px;"></el-input>
							</el-form-item>
						</div>
					</div>
				</el-form>
			</div>
			<div class="d_bottom">
				<div class="d_btn">
					<el-button size="small" plain @click="drawer = false">返回</el-button>
				</div>
				<div class="d_btn">
					<el-button size="small" type="primary" @click="submitForm('form')" :disabled="addTrue">保存
					</el-button>
				</div>
			</div>
		</el-drawer>
	</div>
</template>

<script>
import Top from "../../../components/top.vue"
import utilData from "../../../util/data.js"
export default {
	components: {
		Top,
	},
	data() {
		
		return {
			keywords: "",
			table_height: document.documentElement.clientHeight - 360,
			diaHeight: document.documentElement.clientHeight - 142,
			dataList: [],
			total: 10,
			dialogVisible: false,
			drawer: false,
        	direction: 'rtl',
			form: {
				title: "",
				addr: "",
			},
			rules: {
				title: [
					{ required: true, message: '请输入场地名称', trigger: ['blur,change'] }
				],
				addr: [
					{ required: true, message: '请输入地址', trigger: ['blur,change'] }
				]
			},

			addTrue:false,
			li_num: 1,
			left: 0,
			current: 1,
			limit: 10,
			isEdit:false,
		}

	},
	methods: {

		add(){
			this.$http({
				url: this.$api.serveContentDetail,
					method: 'post',
					data:{
						cataName:utilData.changdiCataname
					}
			}).then(res => {
				if (res.data.code == 20000) {
					const detail = res.data.data.detail;
					if(detail){
						this.form = {
							id:detail.id,
							title: detail.title,
							addr: detail.addr,
						}
					}else{
						this.form = {
							title: "",
							addr: "",
						}
					}
					
					this.drawer = true
				}else{
					this.$message.success(res.data.message)
				}
			})
			
		},
		// 表单验证
		submitForm(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					this.upload()
				}
			});
		},
		// 上传
		upload() {
			this.addTrue = true

			let url = ""
			let data
			const id = this.form.id
			if(id){
				url = this.$api.serveContentEdit
				data = this.form
			}else{
				url = this.$api.serveContentAdd
				data = {...this.form,cataname:utilData.changdiCataname}
			}

			this.$http({
				'method': 'post',
				'url': url,
				data: data
			}).then(res => {

				if (res.data.code == 20000) {
					this.drawer = false
					this.$refs.form.resetFields()
					this.$message({
						message: res.data.message,
						type: 'success'
					})
					this.dataInit()

				} else {
					this.$message.error(res.data.message)
				}
				this.addTrue = false
			})

		},

		// 是否有效
		isOkHandler(item) {
			this.$http({
				url: this.$api.serveCangdiUpdate,
				method: 'post',
				data: {
					id: item.id,
					isContact: item.isContact
				}
			}).then(res => {
				if (res.data.code == 20000) {
					this.$message.success(res.data.message)
				} else {
                    item.isContact = item.isContact==0? 1:0
					this.$message.error(res.data.message)
				}
			})
		},

		// 搜索
		search() {
			this.current = 1
			this.dataInit()
		},
		toIndex() {
			this.$router.push('/server')
		},
		

		dataInit() {
			this.$http({
				'method': 'post',
				'url': this.$api.serveCangdiGetList,
				data: {
					current: this.current,
					limit: this.limit,
					keywords: this.keywords
				}
			}).then(res => {
				if (res.data.code == 20000) {
					this.dataList = res.data.data.list
					this.total = res.data.data.total
				}
			})
		},


		// 分页
		handleCurrentChange(num) {
			this.current = num;
			this.dataInit();
		},

		//图片上传成功后
		handleUploadSuccess(data) {
			this.cropperModel = false;
			this.cropperName = '';
			this.form1.picdir = data.url
		},

		// 删除
		remove(item) {
				this.$confirm('是否删除此信息?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url:this.$api.serveChangdiRemove+item.id
					}).then(res =>{
						if(res.data.code==20000){
							this.$message.success(res.data.message);
							this.dataInit();
						}else{
							this.$message(res.data.message)
						}
					})
				}).catch(() => {
					
				});
			},
	},
	created() {
		this.dataInit()
	}

}
</script>

<style scoped>
@import url(../../../assets/css/class.css);
@import url("../../../assets/css/drawer.css");
>>>.el-drawer__close-btn .el-icon {
	display: block;
}
</style>
